﻿using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Text;

using AL.Entity;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace AL.DAL {

    /// <summary>
    /// HH: 数据操作上下文类
    /// </summary>
    public class ContextAL:DbContext {

        /// <summary>
        /// HH: 短消息 - 操作类
        /// </summary>
        public DbSet<Message> Message { get; set; }


        //private readonly static string CONNECTION_STRING = "name=ContextDAL";
        private readonly static string CONNECTION_STRING = "data source=.;Database=AL_Message;Trusted_Connection=true;";

        public ContextAL()
            : base(CONNECTION_STRING)//不写这个  默认的就是ContextDAL
       {

        }



        /// <summary>
        /// HH: 虚拟 提交 方法(保存修改)
        /// </summary>
        public virtual void Commit() {
            base.SaveChanges();
        }


        /// <summary>
        /// 重建数据表结构的时候，使用此方法重新创建表关系
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            // 移除复数表名的契约
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            // 防止黑幕交易 要不然每次都要访问 EdmMetadata这个表
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

            // 示例代码 =====================
            // modelBuilder.Entity<User>()
            //.HasMany(u => u.Roles)
            //.WithMany(r => r.Users)
            //.Map(m =>
            //{
            //    m.ToTable("RoleMemberships");
            //    m.MapLeftKey("UserName");
            //    m.MapRightKey("RoleName");
            //});
            // 示例代码 =====================
        }




    }


}
